package com.ysjr.anipluginsapp;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;

import com.ysjr.anipluginsapp.manager.PluginsManager;
import com.ysjr.baseinterfacelib.BaseInterface;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * -----------------------------------
 * (☆^ー^☆)
 *
 * @author : Ani_aierJun
 * github: https://github.com/aierjun
 * email: aierjun@qq.com
 * create in : 11:29 2019/5/7
 * -----------------------------------
 */


public class ProxyActivity extends Activity {

    private String className;
    private BaseInterface baseInterface;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        className = getIntent().getStringExtra("className");

        try {
            Class activityClass = getClassLoader().loadClass(className);
            Constructor constructor = activityClass.getConstructor(new Class[]{});
            Object instance = constructor.newInstance(new Object[]{});
            baseInterface = (BaseInterface) instance;
            baseInterface.attach(ProxyActivity.this);

            Bundle bundle = new Bundle();
            baseInterface.onCreate(bundle);


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        baseInterface.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        baseInterface.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        baseInterface.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        baseInterface.onStop();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        baseInterface.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        baseInterface.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        baseInterface.onSaveInstanceState(outState);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        baseInterface.onBackPressed();
    }

    @Override
    public ClassLoader getClassLoader() {
        return PluginsManager.getInstance().getDexClassLoader();
    }

    @Override
    public Resources getResources() {
        return PluginsManager.getInstance().getResources();
    }

    @Override
    public void startActivity(Intent intent) {
        String classNameFrom = intent.getStringExtra("className");
        Intent intent1 = new Intent(this,ProxyActivity.class);
        intent1.putExtra("className",classNameFrom);
        super.startActivity(intent1);
    }
}
